﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DevExpress.AgDataGrid;
using DevExpress.Xpf.Editors;
using ImageUploaderComponent;
using ImageUploaderComponent.Data;

namespace ImageUploaderComponant {
    public partial class UploaderDataGridViewOld : UserControl {
        private const string DisabledTextColumnStyleKey = "DisabledTextColumn";
        private const string EnabledTextColumnStyleKey = "EnabledTextColumn";

        public UploaderDataGridViewOld() {
            InitializeComponent();
            DataContext = new UploaderViewModel();
        }

        private void Test_Click(object sender, RoutedEventArgs e) {
            UploaderViewModel model = (UploaderViewModel) DataContext;

        }



        private void AgDataGridColumn_PrepareCellDisplayElementText(object sender, PrepareCellDisplayElementEventArgs e){
            var gridCell = (AgDataGridCell)e.DisplayElement.Parent;
            var dataObject = (ImageData)gridCell.DataContext;
            if (!dataObject.IsEnabled){
                setCellStyle(e.DisplayElement,DisabledTextColumnStyleKey);
            }
        }



        private void agDataGrid1_CellValueChanged(object sender, CellValueChangedEventArgs e) {
            UploaderViewModel model = (UploaderViewModel)DataContext;
        }

        private void agDataGrid1_CellValueChanging(object sender, CellValueChangingEventArgs e) {
            if(e.Column.FieldName == "IsEnabled"){
                bool isEnabled = (bool) e.Value;
                ((ImageData) agDataGrid1.GetDataRow(e.RowHandle)).IsEnabled = isEnabled;
                if(isEnabled){
                    setEnabledRow(e.RowHandle);
                }
                else{
                    setDisabledRow(e.RowHandle);
                }
            }
        }

        private void setDisabledRow(int rowHandle){
            var row = agDataGrid1.GetRow(rowHandle);
            row.Foreground = new SolidColorBrush(Colors.LightGray);
        }

        private void setEnabledRow(int rowHandle){
            AgDataGridRow row = (AgDataGridRow)agDataGrid1.GetRow(rowHandle);
            row.Foreground = new SolidColorBrush(Colors.Black);
        }



        private void setCellStyle(FrameworkElement cell, string styleKey){
            var style = (Style) Resources[styleKey];
            cell.Style = style;
        }
    }
}
